package cn.easytogo.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.easytogo.bean.User;
import cn.easytogo.bean.Usergroup;
import cn.easytogo.bean.UsergroupAndRole;
import cn.easytogo.service.IUserDAO;

public class UserDAOImpl extends BaseService implements IUserDAO {
	public User findUserByUserName(String username) throws SQLException {
		Map m = new HashMap();
		m.put("username", username);
		User user = (User) this.getBaseDAO().queryForObject("user.findUserByUsername", m);
		return user;
	}

	public User findUserByEmail(String email) throws SQLException {
		Map m = new HashMap();
		m.put("email", email);
		User user = (User) this.getBaseDAO().queryForObject("user.findUserByEmail", m);
		return user;
	}

	public User findUserByUserNameAndPassword(String username, String password)
			throws SQLException {
		Map m = new HashMap();
		m.put("username", username);
		m.put("password", password);
		User user = (User) this.getBaseDAO().queryForObject("user.findUserByUsernameAndPassword", m);
		return user;
	}

	public List selectUsersByMap(Map map) throws SQLException {
		return this.getBaseDAO().queryForList("user.selectUsersByMap", map);
	}

	public int deleteUser(User user) throws SQLException {
		try {
			this.getBaseDAO().startTransaction();
			this.getBaseDAO().delete("user.deleteUser", user);
			Usergroup usergroup = new Usergroup();
			usergroup.setId(user.getId());
			usergroup.setUsergroup_flag("0");
			this.getBaseDAO().delete("usergroup.deleteUsergroupAndRole", usergroup);
			this.getBaseDAO().commitTransaction();
		} finally {
			this.getBaseDAO().endTransaction();
		}
		return 0;
	}
	public Object insertUser(User user) throws SQLException {
		try {
			this.getBaseDAO().startTransaction();
			//保存用户基本信息
			this.getBaseDAO().insert("user.insertUser",user);
			Integer id = (Integer)this.getBaseDAO().queryForObject("usergroup.selectLastInsertId");
			user.setId(id.toString());
			//保存用户的附加角色信息
			Usergroup usergroup = new Usergroup();
			usergroup.setId(id.toString());
			usergroup.setUsergroup_flag("0");
			usergroup.setRoleIds(user.getRoleIds());
			insertUsergroupAndRoles(usergroup);
			this.getBaseDAO().commitTransaction();
		} finally {
			this.getBaseDAO().endTransaction();
		}
		return null;
	}

	public Object updateUser(User user) throws SQLException {
			try {
				this.getBaseDAO().startTransaction();
				this.getBaseDAO().update("user.updateUser",user);
				Usergroup usergroup = new Usergroup();
				usergroup.setId(user.getId());
				usergroup.setRoleIds(user.getRoleIds());
				usergroup.setUsergroup_flag("0");
				this.getBaseDAO().delete("usergroup.deleteUsergroupAndRole", usergroup);
				insertUsergroupAndRoles(usergroup);
				this.getBaseDAO().commitTransaction();
			} finally {
				this.getBaseDAO().endTransaction();
			}
		return null;
	}
	/**
	 * 根据usergroup的roleIds属性批量插入UsergroupAndRole
	 * @throws SQLException 
	 * */
	private void insertUsergroupAndRoles(Usergroup usergroup) throws SQLException{
		String roleIds = usergroup.getRoleIds();
		if(roleIds!=null&&!"".equals(roleIds)){
			String[] roleIdArray = roleIds.split(",");
			for (String rId : roleIdArray) {
				UsergroupAndRole uar = new UsergroupAndRole();
				uar.setUser_id(usergroup.getId());
				uar.setRole_id(rId);
				uar.setUsergroup_flag(usergroup.getUsergroup_flag());
				this.getBaseDAO().insert("usergroup.insertUsergroupAndRole",uar);
			}
		}
	}
}
